<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css" />
</head>
<style>
    html,
    body {
        background-color: #fff;
    }

    .placeholder_div {
        background-color: #fbf9f9;
    }

    .tips {
        padding: 0rem 0.8rem;
        color: #838383;
        background-color: #fff;
        font-size: 0.7rem;
    }

    .tips p:first-child {
        padding-top: 1rem;
    }

    .tips p:last-child {
        margin-top: 0.2rem;
        padding-bottom: 1rem;
        border-bottom: 1px solid #e8e8e8;
    }

    .user_phone,
    .user_psw {
        padding: 0 0.8rem;
        position: relative;
    }

    .user_psw .auth_code {
        position: absolute;
        right: 1rem;
        font-size: 0.5rem;
        color: #838383;
        top: 0.85rem;
        padding: 0.3rem 1rem;
        border-left: 1px solid #e8e8e8;
    }

    .user_phone img,
    .user_psw img {
        width: 1rem;
        height: 1.4rem;
        position: absolute;
        left: 1.5rem;
        top: 0.75rem;
    }

    input {
        width: 85%;
        padding-left: 15%;
        height: 3rem;
        line-height: 3rem;
        border-bottom: 1px solid #eee !important;
        outline: none;
    }

    .queren_btn {
        margin-left: 1.5rem;
        width: 15.75rem;
        height: 2rem;
        line-height: 2rem;
        text-align: center;
        background-color: #2E8EFF;
        color: #fff;
        font-size: 0.8rem;
        margin-top: 2.5rem;
        border-radius: 0.5rem;
    }
</style>

<body>
    <div  id="app" v-cloak>
        <div class="placeholder_div"></div>

        <div class="tips">
            <p>更换手机号后，下次登录可使用新手机号登录</p>
            <p>当前手机号：{{phone_num}}</p>
        </div>

        <div class="user_phone">
            <img src="../image/phone_icon.png" alt="">
            <input type="tel" v-model="tel" maxlength="11" placeholder="请输入您的手机号">
        </div>
        <div class="user_psw">
            <img src="../image/auth_code_icon.png" alt="">
            <input type="text" v-model="code" placeholder="请输入验证码">
            <div class="auth_code" onclick="getCode()">{{get_auth_code}}</div>
        </div>

        <div class="queren_btn" v-if="phone_num == '您还未绑定手机号'" onclick="register()">绑定</div>
        <div class="queren_btn" v-if="phone_num != '您还未绑定手机号'" onclick="save()">确认</div>
    </div>
</body>

</html>
<script src="../script/set_root.js"></script>

<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/zepto.min.js"></script>
<script type="text/javascript" src="../script/hb.js"></script>
<script type="text/javascript" src="../script/vue.js"></script>
<script type="text/javascript">
    apiready = function () {
        hb_ready();
        if (api.pageParam.id == "您还未绑定手机号") {
            toast('首次登录，请绑定手机号')
        }
        app.phone_num = api.pageParam.id
    }
    //使用vue对数据进行绑定
    var app = new Vue({
        el: '#app',
        data: {
            tel: '', //用户输入的手机号
            code: '', //用户输入的验证码
            phone_num: "",
            miaoshu: 60, //倒计时的秒数
            get_auth_code: '获取验证码',
        },
        methods: {

        },

    })

    // 微信登录未绑定手机号
    function register() {
        post_ajax("user/register", {
            identityType: "WECHAT_OPEN",
            identifier: app.tel,
            clientType: "doctor",
            code: app.code,
            socialParams: {
                identifier: api.pageParam.index,
                identityType: "wechatopen",
            }
        }, function (res, err) {
            if (res.code == 200) {
                //  alert(JSON.stringify(res))
                $api.setStorage('emp', res.data);
                toast('登录成功')
                api.sendEvent({
                    name: 'login',
                });
                api.openWin({
                    name: 'main',
                    url: 'main.html',
                    pageParam: {
                        login: 1
                    },
                });
            } else {
                alert(res.msg)
            }
        })
    }

    function save() {
        post_ajax("user/change-mobile", {
            code: app.code,
            identifier: app.tel,
            clientType: "doctor",
            userId: $api.getStorage("emp").userId,
        }, function (res, err) {
            if (res.code == 200) {
                // alert(JSON.stringify(res))
                toast("修改成功")
                setTimeout(function () {
                    api.closeWin()
                }, 500);
            } else {
                alert(JSON.stringify(err))
            }
        })
    }

    //点击获取验证码
    function getCode() {
        if (app.miaoshu != 60) {
            return;
        }
        if (!(/^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/.test(app.tel))) {
            toast('请填写正确的手机号后在获取验证码！');
            return;
        }
        daojishi()

        get_ajax('base/sms/send/code', {
            phone: app.tel,

        }, function (ret, err) {
            // alert(JSON.stringify(ret));
            if (ret.code != 200) {
                app.miaoshu = 1;
            } else {
                toast("网络不通畅，请稍后再试！")
            }
            if (err) {
                alert("网络不通畅，请稍后再试！")
            }
            toast("验证码发送成功");
        });

    }
    //倒计时
    function daojishi() {
        app.get_auth_code = '等待60秒'

        var t = setInterval(function () {
            app.miaoshu--;
            app.get_auth_code = '等待' + app.miaoshu + '秒'
            if (app.miaoshu == 0) {
                clearInterval(t)
                app.get_auth_code = '获取验证码'
                app.miaoshu = 60;
            }

        }, 1000)
    }
</script>
